// mircoApp/brand-market/brand_market/cloud_shop_sharePage/cloud_shop_sharePage.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    landscape: [],
    currentPage: 0,
    imgArr: [],
    info: [],
    // 假数据
    shareData: null,
    shopJson: {
      style: 1,
      couponTxt: "全场七折",
      poster_data: [],
    },
    shopJson2: {
      style: 2,
      couponTxt: "全场七折",
      poster_data: [],
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },
  xrRead() {
    // const info = wx.getWindowInfo();
    // let {
    //   pixelRatio
    // } = info;
    
    const calculatedImageWidth = this.calculateImageWidth(750, 560);
    // console.log(calculatedImageWidth)
    this.setData({
      renderWidth: calculatedImageWidth,
    });
  },
  getScreenWidth() {
    const systemInfo = wx.getWindowInfo();
    return systemInfo.windowWidth;
  },
  calculateImageWidth(designWidth, imageWidth){
    const screenWidth = this.getScreenWidth();
    const ratio = screenWidth / designWidth;
    return Math.round(imageWidth * ratio);
  },
  tapSharePoster() {
    const supported = scene.share.supported;
    console.log('分享海报', supported)
  },
  Onchange(e) {
    this.setData({
      currentPage: e.detail.current,
    });
  },
  TabChange(e) {
    console.log(e);
    this.setData({
      active: e.detail.index,
      currentPage:0
    });
    // if (this.data.shareData.mini_share_type1 == 1) {
    //   let canvasOne = this.selectComponent(".ca1");
    //   canvasOne.showCanvasImage();
    // }
    // if (this.data.shareData.mini_share_type2 == 1) {
    //   let canvasTwo = this.selectComponent(".ca2");
    //   canvasTwo.showCanvasImage();
    // }
  },
  getsrcImg(e) {
    this.setData({
      imgArr: this.data.imgArr.concat(e.detail),
    });
    console.log(this.data.imgArr, 'dddddddd')
  },
  getsrcImg2(e) {
    // this.data.imgArr[1] = e.detail
    this.setData({
      imgArr: this.data.imgArr.concat(e.detail),
    });
    console.log(this.data.imgArr, 'qqqqqqqqqq')
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData();
    // this.miniShare();
    this.xrRead();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    console.log('wwwwwwwwwww')
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    console.log('dddddddddd')
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var value = wx.getStorageSync("yz_uid");
    var mid = "";
    if (value) {
      mid = value;
    }
    return {
      imageUrl: this.data.imgArr[this.data.currentPage],
      path: `/mircoApp/brand-market/brand_market/brand_home/brand_home?id=${this.options.brand_id}&mid=${mid}`,
      title: `分享${this.data.shareData?this.data.shareData.shop_name:''}的店铺`,
    };
  },
  async getData() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.brand-market.frontend.poster.posterList"),
    });
    if (data.result) {
      this.setData({
        info: data.data
      })
    } else {
      app.tips(data.msg);
    }
  },
  async miniShare() {
    let language = wx.getStorageSync("langIndex").en;
    wx.showLoading({
      title: '加载中',
    })
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.brand-market.frontend.decorate.miniShare"),
      data: {
        brand_id: this.options.brand_id
      }
    });
    if (data.result) {
      this.setData({
        shareData: data.data
      })
      if (data.data.mini_share_type1) {
        // 九宫格
        let arr = [];
        this.data.shareData.goods_list.forEach((ele, index) => {
          ele.price = `${language['money']}` + ele.price;
          arr.push(ele)
        });
        this.setData({
          'shopJson.couponTxt': this.data.shareData.brand.share_text,
          'shopJson.poster_data': arr
        })
      }
      if (data.data.mini_share_type2) {
        // 一大两小
        let arr = [];
        this.data.shareData.goods_list.forEach((ele, index) => {
          if (index <= 2) {
            ele.price = `${language['money']}` + ele.price;
            arr.push(ele)
          }
        });
        this.setData({
          'shopJson2.couponTxt': this.data.shareData.brand.share_text,
          'shopJson2.poster_data': arr
        })
      }
      if (this.data.imgArr.length <= 0 && this.data.shareData.mini_share_status == 1) {
        let canvasOne, canvasTwo;
        setTimeout(() => {
          if (this.data.shareData.mini_share_type1 == 1) {
            canvasOne = this.selectComponent(".ca1");
            canvasOne.showCanvasImage();
          }
          if (this.data.shareData.mini_share_type2 == 1) {
            canvasTwo = this.selectComponent(".ca2");
            canvasTwo.showCanvasImage();
          }
          wx.hideLoading()
        }, 1000);

        return
      } else {
        wx.hideLoading()
      }

    } else {
      app.tips(data.msg);
      wx.hideLoading()
    }
  },
  async createPoster(e) {
    let {
      index,
      item
    } = e.currentTarget.dataset;
    if (item.poster) {
      wx.downloadFile({
        url: item.poster,
        success: (res) => {
          wx.showShareImageMenu({
            path: res.tempFilePath,
          })
        }
      })
      return
    }
    wx.showLoading({
      title: '海报生成中',
    })
    this.setData({
      currentInd: index
    })
    let json = {
      brand_id: this.options.brand_id,
      id: this.data.info[this.data.currentInd].id
    };
    if (this.options.brand_collect_id) {
      json.brand_collect_id = this.options.brand_collect_id;
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.brand-market.frontend.poster.createPoster"),
      data: json
    });
    if (data.result) {
      wx.hideLoading();
      this.setData({
        ["info[" + this.data.currentInd + "].poster"]: data.data.url
      })
    } else {
      app.tips(data.msg);
      wx.hideLoading();
    }
  }
});